from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from dhm.models.purchased.purchased import Purchased

class PurchasedView(APIView):
    permission_classes = ([IsAuthenticated])
    def get(self, request):
        photo = request.GET.get('photo', '')
        price = int(request.GET.get('price', 1))
        title = request.GET.get('title', '')
        size = request.GET.get('size', '')
        consumer_id = int(request.GET.get('consumer_id', 1))
        consumer_name = request.GET.get('consumer_name', '')
        consumer_address = request.GET.get('consumer_address', '')
        purchase_time = request.GET.get('purchase_time', '')
        purchase_number = int(request.GET.get('purchase_number', 1))
        purchase_color = request.GET.get('purchase_color', '')
        is_apply = request.GET.get('is_apply', 'false')
        purchaseds = Purchased.objects.all().order_by('-purchase_time')
        data = []
        for purchased in purchaseds:
            data.append({
                'id': purchased.id,
                'photo': purchased.photo,
                'price': purchased.price,
                'title': purchased.title,
                'size': purchased.size,
                'consumer_id': purchased.consumer_id,
                'consumer_name': purchased.consumer_name,
                'consumer_address': purchased.consumer_address,
                'purchase_time': purchased.purchase_time,
                'purchase_number': purchased.purchase_number,
                'purchase_color': purchased.purchase_color,
                'is_apply': purchased.is_apply,
            })
        return Response(data)

    def post(self, request):
        data = request.POST
        photo = data.get('photo', '')
        price = int(data.get('price', 1))
        title = data.get('title', '')
        size = data.get('size', '')
        consumer_id = int(data.get('consumer_id', 1))
        consumer_name = data.get('consumer_name', '')
        consumer_address = data.get('consumer_address', '')
        purchase_time = data.get('purchase_time', '')
        purchase_number = data.get('purchase_number', '')
        purchase_color = data.get('purchase_color', '')
        is_apply = data.get('is_apply', '')
        Purchased.objects.create(photo=photo, price=price, title=title, size=size, consumer_id=consumer_id, consumer_name=consumer_name, consumer_address=consumer_address, purchase_time=purchase_time,purchase_number=purchase_number, purchase_color=purchase_color, is_apply=is_apply)
        return Response({
            'result': "success"
        })

    def patch(self, request):
        purchased_id = int(request.data.get('purchased_id', 1))
        is_apply = request.data.get('is_apply', '')
        purchased = Purchased.objects.get(id=purchased_id)
        purchased.is_apply = is_apply
        purchased.save()
        return Response({
            'result': "success",
        })


